<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 附近模块
 * @package Home\Controller
 */
class NearController extends CommonController {

    /**
     * 获取用户距离经纬度
     */
    public function distance(){
        $longitude = I('longitude');     //经度
        $latitude = I('latitude');       //纬度
        $uid = I('post.uid',0,'intval'); //用户id

        if(!$uid) returnApiError(1,'缺少uid参数');
        if(!$longitude) returnApiError(1,'缺少经度');
        if(!$latitude) returnApiError(1,'缺少纬度');

        $member = M('member');
        $save['longitude'] = $longitude;
        $save['latitude'] = $latitude;
        $data = $member->where("member_id = $uid")->save($save);
        if($data !== false){
            returnApiSuccess('ok');
        }else{
            returnApiError(1,'更新失败');
        }


    }

    /**
     * 搜索附近的人
     */
    public function search(){

        $uid = I('post.uid',0,'intval'); //用户id
        $sex = I('post.sex',0,'intval');//性别
        if(!$uid) returnApiError(1,'缺少uid参数');
        $member = M('member');
        $data = $member->find($uid);
        if(!$data) returnApiError(1,'用户不存在');
        $lng = $data['longitude'];
        $lat = $data['latitude'];
        if($sex <3){
        $where['member_sex']=$sex;
        }
        $where['member_id'] = array('neq',$uid);
        $where['longitude'] = array('neq',"");
        $arr = $member->field('member_id,member_mobile,member_name,member_sex,member_truename,member_avatar,latitude,longitude')->where($where)->select();
        if(!$arr) returnArr('数据为空');

        foreach($arr as $k=>$v){
            $distance = $this->getDistance($lat,$lng,$v['latitude'],$v['longitude']);
            $arr[$k]['distance'] = $distance;
            $arr[$k]['member_avatar'] = SHOP_SITE_URL.'/data/upload/shop/avatar/'. $arr[$k]['member_avatar'];
        }

        $arr =  $this->arr_sort($arr,'distance');
        $i = 1;
        foreach($arr as $k=>$v){
           $output[] = $arr[$k];
           if($i==2){
               break;
           }
           $i++;
        }


        if($output) {
            returnApiSuccess('ok',$output);
        }else{
            returnArr('数据为空');
        }

    }


    function arr_sort($array,$key,$order="asc"){//asc是升序 desc是降序

        $arr_nums=$arr=array();

        foreach($array as $k=>$v){

            $arr_nums[$k]=$v[$key];

        }

        if($order=='asc'){

            asort($arr_nums);

        }else{

            arsort($arr_nums);

        }

        foreach($arr_nums as $k=>$v){

            $arr[$k]=$array[$k];

        }

        return $arr;

    }



    /*
     * 距离计算
     */
    function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters

        /*
          Convert these degrees to radians
          to work with the formula
        */

        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;

        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;

        /*
          Using the
          Haversine formula

          http://en.wikipedia.org/wiki/Haversine_formula

          calculate the distance
        */

        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);  $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;

        return round($calculatedDistance);
    }

    /**
     * 清除地理位置
     */
    public function clear(){
         $uid = I('post.uid',0,'intval');
         if(!empty($uid)){
            $member = M('member');
            $save['longitude'] = 0;
            $save['latitude'] = 0;
            $data = $member->where("member_id = $uid")->save($save);
            returnApiSuccess('ok');
        }else{
            returnApiError(1,'没有该用户!');
        }
         
     }

}